package com.liuzhilin.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.liuzhilin.domain.Article;
import com.liuzhilin.service.UserService;
import com.liuzhilin.util.Response;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/userGetUnapprovedArticles")
public class UserGetUnapprovedArticles extends HttpServlet {
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
            Response.setCorsHeaders(response);
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/json");
            PrintWriter out = response.getWriter();
            try {
                String username = request.getParameter("username");
                username = username.trim();

                List<Article> unapprovedArticles = UserService.getUnapprovedArticles(username);

                ObjectMapper objectMapper = new ObjectMapper();
                objectMapper.writeValue(response.getWriter(), unapprovedArticles);
                response.setStatus(HttpServletResponse.SC_OK);

            } catch (Exception e) {
                e.printStackTrace();
                out.print("{\"success\": false, \"message\": \"服务器内部错误\"}");
                response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            }
        }

        @Override
        public void doOptions(HttpServletRequest request, HttpServletResponse response) {
            Response.setCorsHeaders(response);
            response.setStatus(HttpServletResponse.SC_OK);
        }

}
